package com.sjht.android.bxgz.view;

import android.content.Context;
import android.drm.DrmStore.Action;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

public class PictureGallery extends Gallery implements OnGestureListener {

	private ViewPager mPager;

	public ViewPager getmPager() {
		return mPager;
	}

	public void setmPager(ViewPager mPager) {
		this.mPager = mPager;
	}

	public PictureGallery(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public PictureGallery(Context context, AttributeSet attrs) {
		super(context, attrs); // TODO Auto-generated constructor stub
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		// mPager.requestDisallowInterceptTouchEvent(true);
		return super.dispatchTouchEvent(ev);
	}

	float oldX;

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub

		// mPager.requestDisallowInterceptTouchEvent(false);
		// switch (ev.getAction()) {
		// case MotionEvent.ACTION_MOVE:
		// Log.i("onInterceptTouchEvent", "getSelectedItemId() = " +
		// getSelectedItemId());
		// Log.i("onInterceptTouchEvent", "getChildCount() = " +
		// getChildCount());
		// Log.i("onInterceptTouchEvent", "oldX = " + oldX);
		// Log.i("onInterceptTouchEvent", "ev.getX() = " + ev.getX());
		//
		// if(getSelectedItemId() == 5) {
		// if(oldX < ev.getX())
		// mPager.requestDisallowInterceptTouchEvent(true);
		// }
		// oldX = ev.getX();
		// break;
		//
		// default:
		// break;
		// }

		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		mPager.requestDisallowInterceptTouchEvent(true);
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			Log.i("onInterceptTouchEvent", "getSelectedItemId() = " + getSelectedItemId());
			Log.i("onInterceptTouchEvent", "getChildCount() = " + getChildCount());
			Log.i("onInterceptTouchEvent", "oldX = " + oldX);
			Log.i("onInterceptTouchEvent", "ev.getX() = " + event.getX());

			if (getSelectedItemId() == 5) {
				if (oldX != 0 && oldX > event.getX())
					mPager.requestDisallowInterceptTouchEvent(false);
			}
			if (getSelectedItemId() == 0) {
				if (oldX != 0 && oldX < event.getX())
					mPager.requestDisallowInterceptTouchEvent(false);
			}
			oldX = event.getX();
			break;

		default:
			break;
		}
		return super.onTouchEvent(event);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (e2.getX() > e1.getX()) {
			return super.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
		} else {
			return super.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
		}
	}

}
